<template>
  <div>
    <div class="header" v-if="nodeInfo.id">
      <el-page-header @back="goBack" :content="'知识节点关系管理 - ' + nodeInfo.name"/>
    </div>

    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList"
        :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增关系</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
          >
            <el-table-column prop="source_name" label="源节点" min-width="200"/>
            <el-table-column prop="target_name" label="目标节点" min-width="200"/>
            <el-table-column prop="type" label="关系类型" width="120">
              <template slot-scope="scope">
                {{ getRelationTypeName(scope.row.type) }}
              </template>
            </el-table-column>
            <el-table-column prop="description" label="关系描述" min-width="200"/>
            <el-table-column prop="weight" label="权重" width="100">
              <template slot-scope="scope">
                {{ scope.row.weight.toFixed(2) }}
              </template>
            </el-table-column>

            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" :node-id="nodeId"/>
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "KnowledgeRelationIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      nodeId: null,
      nodeInfo: {},
      request: http.knowledgeRelation,
      searchList: [
        {
          label: '源节点',
          prop: 'source_name',
          name: 'input',
          clearable: true
        },
        {
          label: '目标节点',
          prop: 'target_name',
          name: 'input',
          clearable: true
        },
        {
          label: '关系类型',
          prop: 'type',
          name: 'select',
          clearable: true,
          options: [
            { label: '包含', value: 1 },
            { label: '属于', value: 2 },
            { label: '依赖', value: 3 },
            { label: '相关', value: 4 },
            { label: '引用', value: 5 }
          ]
        }
      ],
      searchParams: {
        source_name: '',
        target_name: '',
        type: ''
      },
      defaultParams: {},
      tableData: []
    }
  },
  created() {
    this.nodeId = this.$route.query.nodeId
    if (this.nodeId) {
      this.getNodeInfo()
      this.searchParams.sourceId = this.nodeId
      this.searchParams.targetId = this.nodeId
    }
  },
  methods: {
    getNodeInfo() {
      http.knowledgeNode.get(this.nodeId).then(res => {
        this.nodeInfo = res.data
      })
    },
    getRelationTypeName(type) {
      const types = {
        1: '包含',
        2: '属于',
        3: '依赖',
        4: '相关',
        5: '引用'
      }
      return types[type] || '-'
    },
    onAdd() {
      this.$refs.addFormRef.open()
    },
    onEdit(row) {
      this.$refs.addFormRef.open(row)
    },
    onDelete(row) {
      this.$confirm('确认删除该知识关系吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        http.knowledgeRelation.delete(row.id).then(() => {
          this.$message.success('删除成功')
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    goBack() {
      this.$router.push('/knowledge/node')
    }
  }
}
</script>

<style scoped lang="scss">
.header {
  margin-bottom: 20px;
}
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
